package com.ruoyi.wx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtExp;
import com.ruoyi.wx.entity.PtReport;
import com.ruoyi.wx.entity.PtScore;
import com.ruoyi.wx.entity.PtVideo;
import com.ruoyi.wx.entity.dto.*;
import com.ruoyi.wx.mapper.PtExpMapper;
import com.ruoyi.wx.mapper.PtReportMapper;
import com.ruoyi.wx.mapper.PtScoreMapper;
import com.ruoyi.wx.mapper.PtVideoMapper;
import com.ruoyi.wx.service.IPtExpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 实验表 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@Service
public class PtExpServiceImpl extends ServiceImpl<PtExpMapper, PtExp> implements IPtExpService {

    @Autowired
    private PtVideoMapper ptVideoMapper;

    @Autowired
    private ISysOssService iSysOssService;

    @Autowired
    private PtReportMapper ptReportMapper;

    @Autowired
    private PtScoreMapper ptScoreMapper;

    @Override
    public List<PtExp> getExpByList(long[] list) {
        return baseMapper.getExpByList(list);
    }

    @Override
    public List<IndexPrepExpDTO> getPrepAndNoPrepExp(long[] ids) {
        Long userId = SecurityUtils.getUserId();
        List<IndexPrepExpDTO> list = baseMapper.getPrepAndNoPrepExp(userId, ids);
        Long aLong = ptVideoMapper.getVideoCount(ids);
        //数据处理
        for (IndexPrepExpDTO temp : list) {
            if (temp.getScorePreview() == null && temp.getVideoList() == null) {
                temp.setAByte(0);
                temp.setIsPreview(false);
                continue;
            }
            if (temp.getScorePreview() != null) {
                temp.setAByte(100);
                temp.setIsPreview(true);
                temp.setScorePreview(temp.getScorePreview());
                continue;
            }
            if (temp.getVideoList() != null) {
                List<Integer> parse = (List<Integer>) JSONObject.parse(temp.getVideoList());
                int size = parse.size();
                temp.setAByte((int) (((float) size / aLong) * 50));
                temp.setIsPreview(false);
            }
        }
        return list;
    }

    @Override
    public void uploadExpPdf(MultipartFile file, Long expId) {
        PtExp ptExp = new PtExp();
        ptExp.setId(expId);
        ptExp.setPdfUrl(iSysOssService.upload(file).getUrl());
        baseMapper.updateById(ptExp);
    }

    @Override
    public List<IndexTreatExpDTO> getTreatAndNoTreatExp(long[] ids) {
        Long userId = SecurityUtils.getUserId();
        List<IndexTreatExpDTO> res = baseMapper.getTreatAndNoTreatExp(userId, ids);
        for (IndexTreatExpDTO re : res) {
            if (re.getEndTime() == null) {
                re.setIsOpen(false);
            } else {
                re.setIsEnd(System.currentTimeMillis() > re.getEndTime().getTime());
                re.setIsOpen(true);
                re.setIsSubmitExpReport(ptReportMapper.selectCount(Wrappers.lambdaQuery(PtReport.class)
                    .eq(PtReport::getExpId, re.getExpId()).eq(PtReport::getUserId, userId)
                ) != 0);
            }
        }
        return res;
    }

    @Override
    public List<IndexEndExpDTO> getEndExp() {
        Long userId = SecurityUtils.getUserId();
        return baseMapper.getEndExp(userId);
    }

    @Override
    public IndexEndExpScoreDTO getEndExpScore(Long expId) {
        Long userId = SecurityUtils.getUserId();
        IndexEndExpScoreDTO res = new IndexEndExpScoreDTO();
        PtScore ptScore = ptScoreMapper.selectOne(Wrappers.lambdaQuery(PtScore.class)
            .eq(PtScore::getExpId, expId).eq(PtScore::getUserId, userId)
        );
        BeanCopyUtils.copyProperties(ptScore, res);

        IndexEndExpReportDTO indexEndExpReportDTO = new IndexEndExpReportDTO();
        PtReport ptReport = ptReportMapper.selectOne(Wrappers.lambdaQuery(PtReport.class)
            .eq(PtReport::getExpId, expId).eq(PtReport::getUserId, userId)
        );
        BeanCopyUtils.copyProperties(ptReport, indexEndExpReportDTO);
        indexEndExpReportDTO.setReportScoreFinal(ptReport.getScoreFinal());
        res.setReportScore(indexEndExpReportDTO);
        return res;
    }
}
